package com.moshme.framework.config;

import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler;
import com.moshme.common.utils.SecurityUtils;
import org.apache.ibatis.reflection.MetaObject;
import org.springframework.stereotype.Component;

import java.time.LocalDateTime;

/**
 * @author Kieyfr
 * @version 1.0
 * @description 配置mybatis-plus的默认值
 * @date 2023/8/23 17:11
 */

@Component
public class MybatisHandler implements MetaObjectHandler {
    @Override
    public void insertFill(MetaObject metaObject) {
        this.setFieldValByName("createTime", LocalDateTime.now(),metaObject);
        this.setFieldValByName( "createBy", SecurityUtils.getLoginUser().getUser().getUserName(),metaObject);
        this.setFieldValByName("updateTime", LocalDateTime.now(),metaObject);
        this.setFieldValByName( "updateBy", SecurityUtils.getLoginUser().getUser().getUserName(),metaObject);
    }

    @Override
    public void updateFill(MetaObject metaObject) {
        this.setFieldValByName("updateTime", LocalDateTime.now(),metaObject);
        this.setFieldValByName( "updateBy", SecurityUtils.getLoginUser().getUser().getUserName(),metaObject);
    }
}
